package com.rtc.call.core.network.repository

import android.content.Context
import android.provider.Settings
import android.util.Log
import com.google.gson.Gson
import com.rtc.call.core.data.ConfigResponse
import com.rtc.call.core.data.request.ConfigRequest
import com.rtc.call.core.network.RetrofitClient

import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn


/**
 * Repository for configuration data
 */
class ConfigRepository(private val context: Context) {
    private val TAG = "ConfigRepository"
    private val configApiService = RetrofitClient.configApiService
    
    /**
     * Get configuration data based on the configuration type
     */
    fun getConfig(request : ConfigRequest): Flow<Result<ConfigResponse>> = flow {

        val response = configApiService.getConfig(request)
        
        if (response.code == 0 && response.data != null) {
            val gson = Gson()
            emit(Result.success(gson.fromJson(response.data,ConfigResponse::class.java)))
        } else {
            emit(Result.failure(Exception("API error: ${response.message}")))
        }
    }.catch { e ->
        Log.e(TAG, "Error fetching configuration", e)
        emit(Result.failure(e))
    }.flowOn(Dispatchers.IO)
    

} 